from network.network import ClientSocket
from network.package import JsonPackage
from exception import DisconnectedError
from view import View

from PyQt5.QtWidgets import *
from PyQt5.QtGui import QGuiApplication

import sys
import traceback
import logging

logging.basicConfig(level=logging.INFO , format='%(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

host = '127.0.0.1'
port = 7290
if len(sys.argv) > 1:
    port = int(sys.argv[1])
try:
    app=QApplication(sys.argv)
    try:
        client = ClientSocket(host, port)
        logger.debug(client.sock)
        logger.info("connected")
    except ConnectionRefusedError as e:
        traceback.print_exc()
        client = None
    view = View(client)
    sys.exit(app.exec_())
except Exception as e:
    traceback.print_exc()
    if client:
        client.close()
